package com.jf.net.gson

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

/**
 * ListTypeAdapter
 * @author JackyFlame
 * @time 2023/10/20 14:42
 * @description
 */
class ListTypeAdapter : JsonDeserializer<List<Any>> {

    override fun deserialize(
        json: JsonElement,
        typeOfT: Type,
        context: JsonDeserializationContext
    ): List<Any> {
        return arrayListOf<Any>().also { list->
            if (json.isJsonArray) {
                json.asJsonArray.forEach {
                    list.add(
                        context.deserialize(
                            it,
                            (typeOfT as ParameterizedType).actualTypeArguments[0]
                        )
                    )
                }
            }
        }
    }
}